package com.shiikong_memory.yez.Arknights;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.shiikong_memory.yez.MySQLAPI.DaoImpl.SklandServer;
import love.forte.simbot.component.kook.event.KookChannelMessageEvent;
import love.forte.simbot.quantcat.common.annotations.ContentTrim;
import love.forte.simbot.quantcat.common.annotations.Listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.CompletableFuture;

@Component
public class NewLogin {
    @Autowired
    SklandServer sklandServer;

    @Autowired
    private SklandAPI sklandAPI;

    /**
     * 登录鹰角通行证监听器
     * @param event 频道消息事件
     * @return 异步发送消息
     */
    @Listener
    @ContentTrim
    public CompletableFuture<?> login(KookChannelMessageEvent event){
        String[] ss;
        if (event.getSourceEvent().getContent().matches("^登录鹰角通行证 \\S+$") & (ss = event.getSourceEvent().getContent().split(" ")).length == 2){
            String id = ss[1];
            if (!sklandAPI.verifyToken(id)){
                return event.replyAsync("鹰角通行证Token不合法请重新获取");
            }else {
                if (sklandServer.getByKookId(event.getAuthor().getId().toString()) == null){
                    boolean b = sklandServer.addSkland(event.getAuthor().getId().toString(), event.getAuthor().getName(), id, event.getBot().getId().toString());
                    if (b) {
                        event.replyAsync("鹰角通行证Token已保存云端,已开启森空岛自动签到功能\n检测到为新用户，开始自动签到");
                    }
                    else return event.replyAsync("鹰角通行证token无法保存到云端，请联系管理者进行修复");
                }else {
                    boolean b = sklandServer.updateByKookId(event.getAuthor().getId().toString(), id);
                    if (b) return event.replyAsync("云端已更新保存鹰角通行证Token");
                    else return event.replyAsync("云端已无法更新保存鹰角通行证Token，请联系管理者进行修复");
                }
                String code = sklandAPI.getCode(id);
                if (code == null){
                    return event.replyAsync("无法获取code，请联系管理者进行修复");
                }
                JSONObject cred = sklandAPI.getCred(code);
                if (cred == null){
                    return event.replyAsync("无法获取cred，请联系管理者进行修复");
                }
                JSONObject jsonObject = sklandAPI.get_binding_list(cred.getString("cred"), cred.getString("token"));
                if (jsonObject == null){
                    event.replyAsync("无法获取游戏角色，请确定游戏角色已创建");
                    return null;
                }
                JSONArray gameList = jsonObject.getJSONObject("data").getJSONArray("list");
                for (int j=0;j<gameList.size();j++){
                    if ("arknights".equals(gameList.getJSONObject(j).getString("appCode"))){
                        JSONArray bingingList = gameList.getJSONObject(j).getJSONArray("bindingList");
                        for (int i=0;i<bingingList.size();i++){
                            JSONObject jsonObject1 = bingingList.getJSONObject(i);
                            boolean b = sklandAPI.sign_in(cred.getString("cred"), cred.getString("token"), jsonObject1.getString("uid"));
                            if (b){
                                return event.replyAsync(jsonObject1.getString("channelName")+"森空岛自动签到成功");
                            }else {
                                return event.replyAsync(jsonObject1.getString("channelName")+"森空岛自动签到失败,可能已签到或鹰角通行证token已失效");
                            }
                        }
                    }
                }
            }
        }
        return null;
    }
}
